package com.dream.controller;

import com.alibaba.fastjson.JSON;
import com.dream.pojo.Department;
import com.dream.pojo.Major;
import com.dream.service.DepartmentService;
import com.dream.service.MajorService;
import com.dream.service.impl.DepartmentServiceImpl;
import com.dream.service.impl.MajorServiceImpl;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/DepartmentController.do")
public class DepartmentController extends BaseServlet{

    DepartmentService departmentService = new DepartmentServiceImpl();
    MajorService majorService=new MajorServiceImpl();

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String deptName = request.getParameter("deptName"); //条件1
        String pageIndex = request.getParameter("pageIndex"); //条件4  当前页码
        if(pageIndex==null||"".equals(pageIndex)){
            pageIndex="1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if(pageSize==null||"".equals(pageSize)){
            pageSize="5";//默认值
        }
        //条数
        long totalCount = departmentService.totalCount(deptName);
        //每页数据
        List<Department> departmentList = departmentService.list(deptName, Long.valueOf(pageIndex), Long.valueOf(pageSize));
        //封装一个分页的工具类
        PageUtil<Department> pageUtil=new PageUtil<Department>(Long.valueOf(pageIndex), Long.valueOf(pageSize),totalCount,departmentList);
        //存值转发
        request.setAttribute("pageUtil",pageUtil);
        request.getRequestDispatcher("viewDeptInfo.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String deptId = request.getParameter("deptId");
        String deptName = request.getParameter("deptName");
        String remark = request.getParameter("remark");
        Department department=new Department(deptId,deptName,remark);
        int count = departmentService.add(department);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"新增成功",null);
        } else {
            resultCommon=new ResultCommon(500,"新增异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void goupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deptId=request.getParameter("deptId");
        request.setAttribute("deptId",deptId);
        Department department = departmentService.findById(deptId);
        request.setAttribute("department",department);
        request.getRequestDispatcher("updateDept.jsp").forward(request,response);
    }


    public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String deptId = request.getParameter("deptId");
        String deptName = request.getParameter("deptName");
        String remark = request.getParameter("remark");
        Department department=new Department(deptId,deptName,remark);
        int count = departmentService.update(department);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String deptId = request.getParameter("deptId");
        //判断这个院系下没有没有专业
        List<Major> majors = majorService.findMajorsByDepartId(deptId);
        if(majors.size()>0){
            response.getWriter().write("<script>alert('院系在使用中，勿删除！');location.href='/DepartmentController.do?action=list';</script>");
        }else{
            int count = departmentService.deleteById(deptId);
            if(count>0){
                response.getWriter().write("<script>alert('院系删除成功');location.href='/DepartmentController.do?action=list';</script>");
            }else{
                response.getWriter().write("<script>alert('院系删除异常');location.href='/DepartmentController.do?action=list';</script>");
            }
        }
    }

    //查询所有的院系
    public void getAllDept(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        List<Department> departmentList= departmentService.getAllDept();
        if (departmentList.size() > 0) {
            resultCommon=new ResultCommon(200,"查询成功",departmentList);
        } else {
            resultCommon=new ResultCommon(500,"查询异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
}
